package com.qdlc.p2p.biz.task.impl;

import javax.annotation.Resource;

import org.apache.log4j.Logger;

import com.qdlc.p2p.biz.service.UserService;
import com.qdlc.p2p.biz.task.AbstractLoanTask;
import com.qdlc.p2p.biz.task.JobQueue;
import com.qdlc.p2p.biz.task.ValueEvent;
import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.exception.BusinessException;
import com.qdlc.p2p.common.util.BeanUtil;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.dao.MemberInfosDao;
import com.qdlc.p2p.dal.dto.MemberInfos;

public class UserTask extends AbstractLoanTask {
    private Logger logger = Logger.getLogger(TenderTask.class);

    @Resource
    private MemberInfosDao memberInfosDao;

    public UserTask() {
        super();
        task.setName("User.Task");
    }

    @SuppressWarnings("unchecked")
    @Override
    public void doLoan() {

    	UserService userService  = (UserService)BeanUtil.getBean("userService");
        JobQueue<ValueEvent> queue = JobQueue.getUserInstance();
        while (!queue.isEmpty()) {
            ValueEvent event = queue.peek();
            if (event != null) {
                String result = "success";
                try {
                    if ("tppRegisger".equals(event.getOperate())) {
                    	userService.tppchinapnrRegis(event.getUserId(), event.getTppUserId(), event.getTppUserCustId(),event.getCardId(),event.getRealName());
                	//企业开户成功
                    } else if ("tppAuditMemberInfosSuccess".equals(event.getOperate())) {
                    	userService.tppAuditMemberInfosSuccess(event.getAuditOrderNo(),event.getInnerOrderNo(),event.getAuditStatus(),event.getAuditMessage());
                	//企业开户失败
                    } else if ("tppAuditMemberInfosFail".equals(event.getOperate())) {
                    	userService.tppAuditMemberInfosFail(event.getAuditOrderNo(),event.getInnerOrderNo(),event.getAuditStatus(),event.getAuditMessage());
					}
                } catch (Exception e) {
                	//更新企业信息表为未申请，清除会员激活信息，重新开户认证
                	QueryParam param = QueryParam.getInstance();
            		param.addParam("auditOrderNo", event.getAuditOrderNo());
            		MemberInfos memberInfos = memberInfosDao.findByCriteriaForUnique(param);
            		if (memberInfos != null) {
            			memberInfos.setInnerOrderNo(event.getInnerOrderNo());
                		memberInfos.setStatus((byte) 0);
                		memberInfos.setAuditMessage(StringUtil.isNull(event.getAuditMessage()));
                		memberInfos.setUpdateTime(DateUtil.getNow());
                		memberInfosDao.update(memberInfos);
            		}
                    logger.error(e.getMessage(), e);
                    if (e instanceof BusinessException) {// 业务异常，保存业务处理信息
                        result = e.getMessage();
                    } else {
                        result = "系统异常，业务处理失败";
                    }
                    MailUtil.sendMail("用户队列异常",e);
                } finally {
                    queue.remove(event);
                }
                if (event.getResultFlag() != null) {// 在需要保存系统处理信息的地方直接保存进来
                    Global.TASK_RESULT_MAP.put(event.getResultFlag(), result);
                }
            }
        }

    }

    @Override
    public Object getLock() {
        return UserTask.USER_STATUS;
    }

}